//
//  CZHomeCell.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/2.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZHomeCell: UITableViewCell {
    
    // 记录约束
    var bottomViewTopConstraint: Constraint?
    
    // 提供一个属性供外界赋值
    var homeViewModel: CZHomeViewModel?{
        didSet{
            // 赋值
            originalView.homeViewModel = homeViewModel

            
            /*
             retweeted_status做判断
             - 如果有值 有转发微博
                - retweetView 赋值
                - bottomView.top = retweetView.Bottom
                - 显示retweetView
             - 如果没有值 没有转发微博
                - bottomView.top = originalView.bottom
                - 隐藏retweetView
             */
            // 卸载约束
            bottomViewTopConstraint?.deactivate()
            
            if homeViewModel?.homeModel?.retweeted_status != nil {
                // 赋值
                retweetView.homeViewModel = homeViewModel
                // 约束
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                // 显示转发微博
                retweetView.isHidden = false
                
            }else{
                // 约束
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
                // 隐藏转发微博
                retweetView.isHidden = true
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(red: 235, green: 235, blue: 235)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        originalView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
//            make.height.equalTo(50)
            make.top.equalTo(8)
        }
        
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.left.right.equalTo(contentView)
//            make.height.equalTo(50)
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
            // 记录约束
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.bottom.equalTo(contentView)
        }
    }
    
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalView: CZHomeOriginalView = CZHomeOriginalView()
    // 转发微博
    private lazy var retweetView: CZHomeRetweetView = CZHomeRetweetView()
    // 底部视图
    private lazy var bottomView: CZHomeBottomView = CZHomeBottomView()

}
